/* ************************************************************************** *
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.
 * 
 * This file is licensed to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * ************************************************************************** */
package org.statewalker.fsm.providers;

import org.statewalker.fsm.IFsmStateProvider;

/**
 * The default implementation of the {@link IFsmStateProvider} interface.
 * Internally it uses {@link IFsmTransitionsRegistry} object which contains a
 * "grammar" of transitions defined in the form of transitions between "static"
 * state keys.
 * 
 * @author kotelnikov
 * @param <S> the type of states
 * @param <E> the type of events
 */
public abstract class FsmStateProvider<S, E> implements IFsmStateProvider<S, E> {

    protected final String fMainStateKey;

    protected IFsmTransitionsRegistry fRegistry;

    public FsmStateProvider(
        IFsmTransitionsRegistry registry,
        String mainStateKey) {
        fRegistry = registry;
        fMainStateKey = mainStateKey;
    }

    /**
     * Returns a "calculated" parent key for the specified event. This method is
     * called only if there is no explicit registered parent given event key.
     * 
     * @param eventKey the key of the event
     * @return a "calculated" parent event key
     * @see #getParentEventKey(String)
     */
    protected String calculateParentEventKey(String eventKey) {
        return FsmBase.ANY_EVENT_KEY;
    }

    protected abstract String getEventKey(E event);

    /**
     * @see org.statewalker.fsm.IFsmStateProvider#getNextState(java.lang.Object,
     *      java.lang.Object, java.lang.Object)
     */
    public S getNextState(S parent, S prev, E event) {
        if (parent == null) {
            return null;
        }
        String parentKey = getStateKey(parent);
        String eventKey = getEventKey(event);
        String targetKey = null;
        String prevKey = getStateKey(prev);
        while (eventKey != null) {
            targetKey = fRegistry.getTransition(parentKey, prevKey, eventKey);
            if (targetKey != null) {
                break;
            }
            targetKey = fRegistry.getDefaultTransition(parentKey, eventKey);
            if (targetKey != null) {
                break;
            }
            eventKey = getParentEventKey(eventKey);
        }
        if (targetKey == null || FsmBase.FINAL.equals(targetKey)) {
            return null;
        }
        return newState(parent, prev, event, targetKey);
    }

    /**
     * Returns the parent key of the event with the specified key.
     * 
     * @param eventKey the key of the event for which the parent should be
     *        returned
     * @return the parent key of the specified event key
     */
    protected String getParentEventKey(String eventKey) {
        if (eventKey == null) {
            return FsmBase.ANY_EVENT_KEY;
        }
        if (FsmBase.ANY_EVENT_KEY.equals(eventKey)) {
            return null;
        }
        String parentKey = fRegistry.getParentEventKey(eventKey);
        if (parentKey == null) {
            parentKey = calculateParentEventKey(eventKey);
        }
        return parentKey;
    }

    public S getRootState(E event) {
        return newState(null, null, event, fMainStateKey);
    }

    protected abstract String getStateKey(S state);

    protected abstract S newState(S parent, S prev, E event, String stateKey);

}